package com.rightsidetech.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.system.dtos.request.store.AddStoreGradeReq;
import com.rightsidetech.system.dtos.request.store.UpdateStoreGradeReq;
import com.rightsidetech.system.dtos.response.store.StoreGradeResp;
import com.rightsidetech.system.entity.store.SysStoreGrade;
import com.rightsidetech.system.mapper.SysStoreGradeMapper;
import com.rightsidetech.system.service.SysStoreGradeService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Package: com.rightsidetech.system.service.impl
 * @ClassName: SysStoreGradeServiceImpl
 * @Author: yinj
 * @CreateTime: 2021/10/21 14:00
 */
@Service("sysStoreGradeServiceImpl")
public class SysStoreGradeServiceImpl implements SysStoreGradeService {

    @Resource
    private SysStoreGradeMapper sysStoreGradeMapper;

    @Override
    public ResponseData<PageInfo<StoreGradeResp>> page(PageRequest request) {
        PageHelper.startPage(request.getPageNo(),request.getPageSize());
        List<StoreGradeResp> list = sysStoreGradeMapper.list();
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public ResponseData add(AddStoreGradeReq req,Long loginInfoId) {

        SysStoreGrade sysStoreGrade =  sysStoreGradeMapper.selectByLevel(req.getLevel());
        if (!ObjectUtils.isEmpty(sysStoreGrade)){
            return ResponseData.fail("等级已存在");
        }
        sysStoreGrade =  sysStoreGradeMapper.selectByLevelName(req.getLevelName());
        if (!ObjectUtils.isEmpty(sysStoreGrade)){
            return ResponseData.fail("等级名称已存在");
        }

        SysStoreGrade levelRule = new SysStoreGrade();
        levelRule.setLevel(req.getLevel());
        levelRule.setLevelName(req.getLevelName());
        levelRule.setCreatorId(loginInfoId);
        levelRule.setCreatedAt(new Date());
        sysStoreGradeMapper.insertSelective(levelRule);
        return ResponseData.success();
    }

    @Override
    public ResponseData update(UpdateStoreGradeReq req, Long loginInfoId) {

        SysStoreGrade sysStoreGrade =  sysStoreGradeMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(sysStoreGrade)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }

        if (!StringUtils.isEmpty(req.getLevel())){
            sysStoreGrade =  sysStoreGradeMapper.selectByLevel(req.getLevel());
            if (!ObjectUtils.isEmpty(sysStoreGrade) && !sysStoreGrade.getId().equals(req.getId())){
                return ResponseData.fail("等级已存在");
            }
        }

        if (!StringUtils.isEmpty(req.getLevelName())){
            sysStoreGrade =  sysStoreGradeMapper.selectByLevelName(req.getLevelName());
            if (!ObjectUtils.isEmpty(sysStoreGrade) && !sysStoreGrade.getId().equals(req.getId())){
                return ResponseData.fail("等级名称已存在");
            }
        }


        SysStoreGrade levelRule = new SysStoreGrade();
        levelRule.setLevel(req.getLevel());
        levelRule.setLevelName(req.getLevelName());
        levelRule.setCreatorId(loginInfoId);
        levelRule.setCreatedAt(new Date());
        levelRule.setId(req.getId());
        sysStoreGradeMapper.updateByPrimaryKeySelective(levelRule);
        return ResponseData.success();
    }

    @Override
    public ResponseData del(UpdateStoreGradeReq req, Long loginInfoId) {
        SysStoreGrade sysStoreGrade =  sysStoreGradeMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(sysStoreGrade)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        sysStoreGradeMapper.deleteByPrimaryKey(req.getId());
        return ResponseData.success();
    }

    @Override
    public SysStoreGrade selectById(Long id) {
        return sysStoreGradeMapper.selectByPrimaryKey(id);
    }
}
